package com.lnking.lnkingplace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lnking.lnkingplace.Util.LocalDateTimeUtil;
import com.lnking.lnkingplace.Util.UserUtil;
import com.lnking.lnkingplace.mapper.IsolationPlaceMapper;
import com.lnking.lnkingplace.mapper.IsolationWorkerMapper;
import com.lnking.lnkingplace.model.dto.isolation.worker.WorkerImportDto;
import com.lnking.lnkingplace.model.entity.IsolationPlace;
import com.lnking.lnkingplace.model.entity.IsolationWorker;
import com.lnking.lnkingplace.model.entity.Leaders;
import com.lnking.lnkingplace.model.enums.DateFormat;
import com.lnking.lnkingplace.model.request.isolation.worker.WorkerAddRequest;
import com.lnking.lnkingplace.model.request.isolation.worker.WorkerUpdateRequest;
import com.lnking.lnkingplace.service.IIsolationWorkerService;
import com.lnking.starter.export.utils.ImportOption;
import com.lnking.starter.export.utils.ImportUtils;
import com.lnking.starter.mybatis.service.impl.CustomServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangqt
 * @since 2022-08-27
 */
@Service
public class IsolationWorkerServiceImpl extends CustomServiceImpl<IsolationWorkerMapper, IsolationWorker> implements IIsolationWorkerService {

    @Resource
    UserUtil userUtil;
    @Resource
    IsolationPlaceMapper isolationPlaceMapper;

    @Override
    public void add(WorkerAddRequest params) throws Exception {
        IsolationWorker info = new IsolationWorker();
        BeanUtils.copyProperties(params,info);
        this.baseMapper.insert(info);
    }

    @Override
    public void update(WorkerUpdateRequest params) throws Exception {
        IsolationWorker info = new IsolationWorker();
        BeanUtils.copyProperties(params,info);
        this.baseMapper.updateById(info);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(String placeId, MultipartFile file) throws Exception {
        ImportOption option = new ImportOption.Builder().build();
        List<WorkerImportDto> result =  ImportUtils.importExcel(file, WorkerImportDto.class,option);
        for (WorkerImportDto dto : result){
            if(StringUtils.isNotBlank(dto.getWorkerName())){
                IsolationWorker info = new IsolationWorker();
                BeanUtils.copyProperties(dto,info);
                placeId = isolationPlaceMapper.getPlaceId(dto.getPlaceName());
                Assert.isTrue(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(placeId), "该场所不存在："+"【"+dto.getPlaceName()+"】");
                info.setPlaceId(placeId);
                info.setOffline("在线");
                if(StringUtils.isNotBlank(dto.getStartDate())){
                    info.setStartDate(LocalDateTimeUtil.stringToLocalDate(dto.getStartDate(), DateFormat.LOCAL_DATE));
                }
                if(StringUtils.isNotBlank(dto.getEndDate())){
                    info.setEndDate(LocalDateTimeUtil.stringToLocalDate(dto.getEndDate(), DateFormat.LOCAL_DATE));
                }
                info.setCreateTime(LocalDateTime.now());
                info.setPassword( DigestUtils.sha1Hex(UserServiceImpl.PWD));
                this.baseMapper.insert(info);
            }
        }
    }

    private String getPlaceId(String placeId){
        if(com.baomidou.mybatisplus.core.toolkit.StringUtils.isBlank(placeId)){
            String name = userUtil.getUser().getName();
            return isolationPlaceMapper.getPlaceId(name);
        }
        return placeId;
    }

    @Override
    public void updateOffline() {
        this.baseMapper.updateOffline();
    }

    @Override
    public IsolationWorker getWorkeByName(String username) {
        QueryWrapper<IsolationWorker> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(IsolationWorker::getWorkerPhone, username);
        wrapper.lambda().eq(IsolationWorker::getOffline,"在线");
        return getBaseMapper().selectOne(wrapper);
    }
}
